package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.CType;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;

/**
 * Class which represents 'Restauracija' from the chart. Timestamp! <br>
 * value represents file number where details about Restoration are stored.
 * 
 * @author Lab
 * 
 */
public class Restoration extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_IDENTIFIER = "identifier";
    public static final String ONT_TIMESPAN = "time-span";
    public static final String ONT_DATE = "date";
    public static final String ONT_MODIFICATION = "modification";

    private String date;

    public Restoration(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null && this.date == null) {
            return;
        }

        Ont.typeCheck(CType.RESTORATION);

        final Query q = new Query();
        String[] result = null;
        final String itemURI = Item.formatItemURI(this.id);

        if (this.value != null && this.date != null) {
            result = q.select("mod").type("mod", CIDOC.E11.queryValue())
                    .connect("mod", CIDOC.P2.queryValue(), CType.RESTORATION.queryValue())
                    .connect("mod", CIDOC.P48.queryValue(), "id").type("id", CIDOC.E42.queryValue())
                    .label("id", this.value).connect("mod", CIDOC.P4.queryValue(), "timeSpan")
                    .type("timeSpan", CIDOC.E52.queryValue()).connect("timeSpan", CIDOC.P78.queryValue(), "time")
                    .type("time", CIDOC.E50.queryValue()).label("time", this.date).find();
        }

        String modURI;
        if (result == null || result.length == 0) {
            modURI = Ont.formatQueryURI(this.id, ONT_MODIFICATION);
            final String identifierURI = Ont.formatURI(this.id, ONT_MODIFICATION, ONT_IDENTIFIER);
            final String timeSpanURI = Ont.formatURI(this.id, ONT_MODIFICATION, ONT_TIMESPAN);
            final String dateURI = Ont.formatURI(this.id, ONT_MODIFICATION, ONT_DATE);

            Ont.getOntology().createIndividual(CIDOC.E11.getFullValue(), modURI);
            Ont.getOntology().connectIndividuals(modURI, CIDOC.P2.getFullValue(), CType.RESTORATION.getURI());

            // id
            if (this.value != null) {
                Ont.getOntology().createIndividual(CIDOC.E42.getFullValue(), identifierURI);
                Ont.getOntology().addLabel(identifierURI, this.value);
                Ont.getOntology().connectIndividuals(modURI, CIDOC.P48.getFullValue(), identifierURI);
            }

            // date
            if (this.date != null) {
                Ont.getOntology().createIndividual(CIDOC.E52.getFullValue(), timeSpanURI);
                Ont.getOntology().createIndividual(CIDOC.E50.getFullValue(), dateURI);
                Ont.getOntology().addLabel(dateURI, this.date);
                Ont.getOntology().connectIndividuals(modURI, CIDOC.P4.getFullValue(), timeSpanURI);
                Ont.getOntology().connectIndividuals(timeSpanURI, CIDOC.P78.getFullValue(), dateURI);
            }
        } else {
            modURI = result[0];
        }

        // connect to item
        Ont.getOntology().connectIndividuals(modURI, CIDOC.P31.getFullValue(), itemURI);

    }

    public void fromRDF() {
        final String itemQueryURI = Item.formatItemQueryURI(this.id);
        final Query query = new Query();

        // file number
        String[] result = query.selectLabel().type("mod", CIDOC.E11.queryValue())
                .connect("mod", CIDOC.P31.queryValue(), itemQueryURI)
                .connect("mod", CIDOC.P2.queryValue(), CType.RESTORATION.queryValue())
                .type("id", CIDOC.E42.queryValue()).connect("mod", CIDOC.P48.queryValue(), "id").findLabel("id");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }

        // date
        result = query.selectLabel().type("mod", CIDOC.E11.queryValue())
                .connect("mod", CIDOC.P31.queryValue(), itemQueryURI)
                .connect("mod", CIDOC.P2.queryValue(), CType.RESTORATION.queryValue())
                .type("timeSpan", CIDOC.E52.queryValue()).connect("mod", CIDOC.P4.queryValue(), "timeSpan")
                .type("date", CIDOC.E50.queryValue()).connect("timeSpan", CIDOC.P78.queryValue(), "date")
                .findLabel("date");
        if (result != null && result.length > 0) {
            this.date = result[0];
        }

    }

    public String getDate() {
        return this.date;
    }

    public void setDate(final String date) {
        this.date = date;
    }

    @Override
    public void toIndexDocument(final Document doc) {
        // Nothing to index
    }

}
